package boot.spring.type.ttl.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class TTLQueueConfig {
    //普通交换机名称
    public static final String NORMAL_EXCHANGE = "NORMAL_EXCHANGE";
    //死信交换机名称
    public static final String DEAD_EXCHANGE = "DEAD_EXCHANGE";
    //普通队列名称
    public static final String NORMAL_QUEUE_A = "NORMAL_QUEUE_A";
    public static final String NORMAL_QUEUE_B = "NORMAL_QUEUE_B";
    //死信队列名称
    public static final String DEAD_QUEUE = "DEAD_QUEUE";

    //通用队列名称
    public static final String NORMAL_QUEUE_C = "NORMAL_QUEUE_C";

    //声明交换机
    @Bean("nExchange")
    public DirectExchange normalExchange(){
        return new DirectExchange(NORMAL_EXCHANGE);
    }
    @Bean("dExchange")
    public DirectExchange deadExchange(){
        return new DirectExchange(DEAD_EXCHANGE);
    }

    //声明普通队列
    @Bean("queueA")
    public Queue queueA(){
        Map<String, Object> arguments = new HashMap<>();
        //设置死信交换机
        arguments.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        //设置死信routingKey
        arguments.put("x-dead-letter-routing-key","YD");
        arguments.put("x-message-ttl",10000);

        return QueueBuilder.durable(NORMAL_QUEUE_A).withArguments(arguments).build();
    }
    @Bean("queueB")
    public Queue queueB(){
        Map<String, Object> arguments = new HashMap<>();
        //设置死信交换机
        arguments.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        //设置死信routingKey
        arguments.put("x-dead-letter-routing-key","YD");
        arguments.put("x-message-ttl",40000);

        return QueueBuilder.durable(NORMAL_QUEUE_B).withArguments(arguments).build();
    }
    //声明死信队列
    @Bean("queueD")
    public Queue queueD(){
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }

    //交换机和队列绑定
    @Bean
    public Binding queueABindingNExchange(@Qualifier("queueA") Queue queueA ,
                                          @Qualifier("nExchange") DirectExchange nExchange){

        return BindingBuilder.bind(queueA).to(nExchange).with("XA");
    }
    @Bean
    public Binding queueBBindingNExchange(@Qualifier("queueB") Queue queueB ,
                                          @Qualifier("nExchange") DirectExchange nExchange){

        return BindingBuilder.bind(queueB).to(nExchange).with("XB");
    }
    @Bean
    public Binding queueDBindingDExchange(@Qualifier("queueD") Queue queueD ,
                                          @Qualifier("dExchange") DirectExchange dExchange){

        return BindingBuilder.bind(queueD).to(dExchange).with("YD");
    }
}